#!/bin/sh
#refer to:http://www.thegeekstuff.com/2010/07/bash-string-manipulation/
#字符串操作
var="Welcome to the geekstuff"
echo string var:${var}
#字符串长度  ${#string}
echo length of string var:${#var}

#子字符串  ${string:position}  ${string:position:length}
echo substring1:${var:15}
echo substring2:${var:15:5}

#最短子字符串匹配
filename="bash.string.txt"
#从前面删除最短匹配	${string#substring}
echo ${filename#*.}
#从后面删除最短匹配	${string%substring}
echo ${filename%.*}

#最长子字符串匹配
filename="bash.string.txt"
#从前面删除最长匹配	${string##substring}
echo ${filename##*.}
#从后面删除最长匹配	${string%%substring}
echo ${filename%%.*}

#查找及替换
#替换第一个匹配项	${string/pattern/replacement}
filename="bash.string.txt"
echo "After Replacement:" ${filename/str*./operations.}
#替换所有匹配项		${string//pattern/replacement}
filename="Path of the bash is /bin/bash"
echo "After Replacement:" ${filename//bash/sh}

#替换开始和结尾的匹配
filename="/root/admin/monitoring/process.sh"
#如果filename以a/root..开头，则不会进行替换	${string/#pattern/replacement
echo "Replaced at the beginning:" ${filename/#\/root/\/tmp}
#当结尾匹配时才替换		${string/%pattern/replacement
echo "Replaced at the end": ${filename/%.*/.ksh}
